﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eoffice.Models;

namespace Eoffice.Controllers
{
    public class KyController : Controller
    {
        private EofficeDb db = new EofficeDb();

        //
        // GET: /Ky/

        public ActionResult Index()
        {
            var kies = db.Kies.Include(k => k.CVDI).Include(k => k.NV);
            return View(kies.ToList());
        }

        //
        // GET: /Ky/Details/5

        public ActionResult Details(int id = 0)
        {
            KY ky = db.Kies.Find(id);
            if (ky == null)
            {
                return HttpNotFound();
            }
            return View(ky);
        }

        //
        // GET: /Ky/Create

        public ActionResult Create()
        {
            ViewBag.SOCVDI = new SelectList(db.CVDIs, "SOCVDI", "SOCVDI");
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV");
            return View();
        }

        //
        // POST: /Ky/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(KY ky)
        {
            if (ModelState.IsValid)
            {
                db.Kies.Add(ky);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.SOCVDI = new SelectList(db.CVDIs, "SOCVDI", "SOCVDI", ky.SOCVDI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", ky.MSNV);
            return View(ky);
        }

        //
        // GET: /Ky/Edit/5

        public ActionResult Edit(int id = 0)
        {
            KY ky = db.Kies.Find(id);
            if (ky == null)
            {
                return HttpNotFound();
            }
            ViewBag.SOCVDI = new SelectList(db.CVDIs, "SOCVDI", "SOCVDI", ky.SOCVDI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", ky.MSNV);
            return View(ky);
        }

        //
        // POST: /Ky/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(KY ky)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ky).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.SOCVDI = new SelectList(db.CVDIs, "SOCVDI", "SOCVDI", ky.SOCVDI);
            ViewBag.MSNV = new SelectList(db.NVs, "MSNV", "HOTENNV", ky.MSNV);
            return View(ky);
        }

        //
        // GET: /Ky/Delete/5

        public ActionResult Delete(int id = 0)
        {
            KY ky = db.Kies.Find(id);
            if (ky == null)
            {
                return HttpNotFound();
            }
            return View(ky);
        }

        //
        // POST: /Ky/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            KY ky = db.Kies.Find(id);
            db.Kies.Remove(ky);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}